package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysThird;
import com.ruoyi.system.service.ISysThirdService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @author renzhihao
 * @date 2023/11/30 13:45
 */
@Controller
@RequestMapping("system/third")
public class SysThirdController extends BaseController {

    private final String prefix = "system/third";

    @Autowired
    private ISysThirdService thirdService;

    @GetMapping
    @RequiresPermissions("system:third:view")
    public String third(ModelMap mmap) {
        SysThird third = thirdService.selectThirdAuth();
        third.setAuthTypeList(Arrays.stream(third.getAuthType().split(",")).collect(Collectors.toList()));

        mmap.put("third", third);
        return prefix + "/third";
    }

    @RequiresPermissions("system:third:edit")
    @Log(title = "第三方认证管理", businessType = BusinessType.UPDATE)
    @PostMapping
    @ResponseBody
    public AjaxResult edit(SysThird third) {
        third.setAuthType(String.join(",", third.getAuthTypeList()));
        return toAjax(thirdService.updateThirdAuth(third));
    }

}
